//+------------------------------------------------------------------+
//| 数组索引测试版本 - 验证MT4/MT5数组访问差异                           |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_label1  "Forward Line"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_width1  2

#property indicator_label2  "Backward Line"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_width2  2

// 缓冲区
double ForwardBuffer[];    // 正向填充 (0->rates_total-1)
double BackwardBuffer[];   // 反向填充 (rates_total-1->0)

//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0, ForwardBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, BackwardBuffer, INDICATOR_DATA);
   
   // 关键测试：不设置ArraySetAsSeries，看看原始访问方式
   // ArraySetAsSeries(ForwardBuffer, true);
   // ArraySetAsSeries(BackwardBuffer, true);
   
   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   if(rates_total < 10) return 0;
   
   int limit = prev_calculated == 0 ? rates_total - 1 : rates_total - prev_calculated;
   if(prev_calculated > 0) limit++;
   
   Print("=== 数组访问测试 ===");
   Print("rates_total=", rates_total, ", limit=", limit);
   
   // 测试1：正向填充 (从0到rates_total-1)
   for(int i = 0; i <= limit; i++)
   {
      if(i < rates_total)
         ForwardBuffer[i] = close[i];
   }
   
   // 测试2：反向填充 (从limit到0) - MT4方式
   for(int i = limit; i >= 0; i--)
   {
      if(i < rates_total)
         BackwardBuffer[i] = close[i];
   }
   
   // 详细调试输出
   if(prev_calculated == 0)
   {
      Print("=== 数组填充结果对比 ===");
      Print("索引方向检查 (前10个点):");
      for(int j = 0; j < MathMin(10, rates_total); j++)
      {
         Print("Index[", j, "] close=", close[j], 
               " forward=", ForwardBuffer[j], 
               " backward=", BackwardBuffer[j]);
      }
      
      Print("=== 时间序列检查 ===");
      Print("time[0]=", TimeToString(time[0]));
      Print("time[1]=", TimeToString(time[1]));
      Print("time[", rates_total-1, "]=", TimeToString(time[rates_total-1]));
      
      // 检查哪种方式产生连续线条
      Print("=== 连续性分析 ===");
      bool forwardConnected = true;
      bool backwardConnected = true;
      
      for(int j = 1; j < MathMin(5, rates_total); j++)
      {
         double forwardDiff = MathAbs(ForwardBuffer[j] - ForwardBuffer[j-1]);
         double backwardDiff = MathAbs(BackwardBuffer[j] - BackwardBuffer[j-1]);
         
         if(forwardDiff == 0) forwardConnected = false;
         if(backwardDiff == 0) backwardConnected = false;
         
         Print("Point[", j, "] ForwardDiff=", forwardDiff, " BackwardDiff=", backwardDiff);
      }
      
      Print("Forward连续性: ", (forwardConnected ? "正常" : "异常"));
      Print("Backward连续性: ", (backwardConnected ? "正常" : "异常"));
   }
   
   return rates_total;
}

//+------------------------------------------------------------------+